﻿Public Class frmClients
    Private szSQL As String
    Private rs As ADODB.Recordset = Nothing

    Private Sub frmClients_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        TE_NOM.Clear()
        Me.BackColor = getSocieteColor()
        InitListeCLIENTS(0)
    End Sub

    Private Sub InitListeCLIENTS(ByVal ix As Integer)

        Dim nb As Integer = 0

        ' effacer la listview
        LV_CLIENTS.Items.Clear()

        ' affiche les 50 CABINETS à partir de l'élève courant
        szSQL = "CALL spGet_CLIENTSbyNOM('" & gSocieteCode & "','" & TE_NOM.Text & "')"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)

        Do While Not rs.EOF
            LV_CLIENTS.Items.Add(Format(rs.Fields("cd_cli").Value, "000000"))
            LV_CLIENTS.Items(nb).SubItems.Add(rs.Fields("nom").Value)
            LV_CLIENTS.Items(nb).SubItems.Add(nv(rs.Fields("nocompte").Value))
            LV_CLIENTS.Items(nb).SubItems.Add(nv(rs.Fields("type").Value))
            LV_CLIENTS.Items(nb).SubItems.Add(nv(rs.Fields("groupe").Value))
            LV_CLIENTS.Items(nb).SubItems.Add(nv(rs.Fields("adresse").Value))
            LV_CLIENTS.Items(nb).SubItems.Add(nv(rs.Fields("ville").Value))
            If rs.Fields("nocompte").Value > 0 Then
                LV_CLIENTS.Items(nb).ForeColor = Color.Green
            End If
            nb = nb + 1
            ' Get the next record
            rs.MoveNext()
        Loop
        ' Close the recordset AND connection.
        rs.Close()

        LB_NbLignes.Text = CStr(nb) & " fiche(s)"
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_CLIENTS)

    End Sub

    Private Sub LV_CLIENTS_DoubleClick(sender As Object, e As System.EventArgs) Handles LV_CLIENTS.DoubleClick

        If LV_CLIENTS.SelectedItems.Count > 0 Then
            ' afficher les détails 
            frmClientMAJ.pCd_cli = LV_CLIENTS.SelectedItems(0).Text
            frmClientMAJ.ShowDialog()
            ' réaffiche la viewlist
            InitListeCLIENTS(LV_CLIENTS.SelectedItems(0).Index)
        End If

    End Sub

    Private Sub LV_CLIENTS_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles LV_CLIENTS.KeyDown

        Select Case e.KeyCode
            Case Keys.Enter         ' Enter = DoubleClick
                LV_CLIENTS_DoubleClick(sender, e)

            Case Keys.Insert        ' Insert = ' Ajouter un élève
                BO_ADD_LIGNE_Click(sender, e)
        End Select

    End Sub

    Private Sub BO_ADD_LIGNE_Click(sender As System.Object, e As System.EventArgs) Handles BO_ADD_LIGNE.Click
        ' création cabinet
        frmClientMAJ.pCd_cli = 0
        frmClientMAJ.ShowDialog()
        InitListeCLIENTS(0)
    End Sub

    Private Sub TE_NOM_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_NOM.KeyDown

        If e.KeyCode = Keys.Enter Then
            ' affiche la listview
            InitListeCLIENTS(0)
        End If

    End Sub
End Class